﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessYaxMachine
// 文件功能描述：设备管理数据访问层
//
// 
// 创建标识：2012-02-13 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessYaxMachine
    {
        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelYaxMachineInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY MacID ASC) rownumber,* " +
                               "from SYS_MACHINES_VIEW) B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString() + strquery;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取设备组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetNodeName()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select NodeID,Nodename from Yax_Node order by NodeID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取设备类型列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetMacTypeName()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select MacTypeID,MacTypeName from Sys_mactype";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 设备数量
        /// </summary>
        /// <returns></returns>
        public int GetYaxMachineCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from SYS_MACHINES_VIEW";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteYaxMachine(string id)
        {
            sqldb db = new sqldb();
            string query = "select COUNT(*) from YAX_MACHINE where (MacNo='100' or MacNo='200') and MacID=" + id;
            object obj = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            if (Convert.ToInt32(obj) > 0)
            {
                return -2;
            }
            query = "Delete from Yax_Machine where MacID = '" + id + "'";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        public int SaveYaxMachine(object[] obj)
        {
            sqldb db = new sqldb();
            object objid = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, "select MAX(MacID)+1 from Yax_Machine");
            string query = "insert into Yax_Machine(MacID,NodeID,MacNo,MacDescribe,MacType,CommStyle,PortNo,BaudRate,IPAddress,GetwayIP,SubIPAddr) Values('" + objid + "','" +
                obj[0] + "','" + obj[1] + "','" + obj[2] + "','" + obj[3] + "','" + obj[4] + "','" + obj[5] + "','" + obj[6] + "','" + obj[7] + "','" + obj[8] + "','" + obj[9] + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条设备信息
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public DataSet GetYaxMachineInfoOne(string id)
        {
            sqldb db = new sqldb();
            string query = "select * from Yax_Machine where MacID = '" + id + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 保存设备(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveYaxMachineEdit(object[] obj)
        {
            sqldb db = new sqldb();
            string query = "update Yax_Machine set NodeID='" + obj[1] + "',MacNo='" + obj[2] + "',MacDescribe='" +
                obj[3] + "',MacType='" + obj[4] + "',CommStyle='" + obj[5] + "',PortNo='" +
                obj[6] + "',BaudRate='" + obj[7] + "',IPAddress='" + obj[8] + "',GetwayIP='" + obj[9] + "',SubIPAddr='" + obj[10] + "' where MacID='" + obj[0] + "'";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        public int CheckMacNo(int MacNo, int OldMacNo, string AddEdit)
        {
            sqldb db = new sqldb();
            string query = "select COUNT(*) from Yax_Machine where MacNo=" + MacNo;
            if (AddEdit == "Edit")
            {
                query += " and MacNo!=" + OldMacNo;
            }
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        public int CheckMacNoAdd(int MacNoAdd)
        {
            sqldb db = new sqldb();
            string query = "select COUNT(*) from Yax_Machine where MacNo=" + MacNoAdd;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
        

        /// <summary>
        /// 检查是否为系统保留项
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int SelYaxMachineEdit(string ID)
        {
            sqldb db = new sqldb();
            string query = "select COUNT(*) from YAX_MACHINE where (MacNo='100' or MacNo='200') and MacID=" + ID;
            object obj = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(obj);
        }
    }
}
